package com.sdp.wx.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_assets_maintain.entity.WxAssetsMaintain;
import com.sdp.wx.db.wx_assets_maintain.service.IWxAssetsMaintainService;
import com.sdp.wx.utils.CurrentLoginEmp;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/WxAssetsMaintainController")
public class WxAssetsMaintainController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxAssetsMaintainService assetsMaintainService;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(assetsMaintainService, WxAssetsMaintain.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxAssetsMaintain assetsMaintain) {
		if(assetsMaintain == null){
			return Result.error("信息为空");
		}
		JSONObject emp = currentLoginEmp.getCurrentLoginInfo();
		if (emp == null || !emp.containsKey("empNo")) {
			return Result.error("当前登录人信息为空");
		}
		if(StringUtils.isEmpty(assetsMaintain.getKeyId())) {
			assetsMaintain.setEmpNo(emp.getString("empNo"));
			assetsMaintain.setEmpName(emp.getString("empName"));
			assetsMaintain.setCreateTime(LocalDateTime.now());
			assetsMaintainService.save(assetsMaintain);

		} else {
			assetsMaintainService.updateById(assetsMaintain);
		}
		
		return Result.ok(assetsMaintain);
	}
	
	@GetMapping(value = "/del")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		assetsMaintainService.removeById(keyId);
		return Result.ok();
	}
	
}

